#!/bin/bash
source ./configurationSettings;
IFS=$'\n';
for LINE in $(cat apTargets.txt); do
    SSID=$(echo $LINE | awk '{ print $3 }');
    CHANNEL=$(echo $LINE | awk '{ print $2 }');
    # sed -r s/-.*-// - in case that comment '-----PIN-WAS-FOUND---- is added
    # sed s/#// - in case that line is preceded by comment sign
    MAC=$(echo $LINE | awk '{ print $1 }' | sed s/#// | sed -r s/-.*-//);
    BSSID_CLEAR=$(echo $MAC | sed s/://g | tr '[:lower:]' '[:upper:]');
    SESSIONFILE=$REAVER_SESSION_DIR'/'$BSSID_CLEAR'.wpc';
    if [[ ! -f "$SESSIONFILE" ]]; then
	echo "------- $SSID (Couldn't find session: $SESSIONFILE) ";
	continue;
    fi
    FIRST_PINS=$(sed '1!d' $SESSIONFILE);
    SECOND_PINS=$(sed '2!d' $SESSIONFILE);
    FIRST_PINS_LINE=$[ $FIRST_PINS + 3 ];
    SECOND_PINS_LINE=$[ $SECOND_PINS + 10004 ];
    LAST_FIRST_PIN=$(sed "$FIRST_PINS_LINE!d" $SESSIONFILE);
    LAST_SECOND_PIN=$(sed "$SECOND_PINS_LINE!d" $SESSIONFILE);
    if [[ -z $FIRST_PINS || -z $SECOND_PINS ]]; then
      echo "------- $SSID (No checked PINs)";
    else 
      ALL_PINS=$[ $FIRST_PINS+$SECOND_PINS ];
      echo "------- $SSID (PINs checked: $ALL_PINS) ---------";
      echo "------- LAST CHECKED PIN PARTS: $LAST_FIRST_PIN, $LAST_SECOND_PIN ---------";
    fi
    ./showPinDates $MAC --group-by-day
done;

